// pages/information/index.js
const {
  information
} = require('../../api/index')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    IMGBASEURL: app.globalData.IMGBASEURL,
    dynamicStyle: "",
    dayStyle: 0,
    list: [], //数据
    PageIndex: 1,
    PageSize: 10,
    loading: false,
  },
  particularMesge(e) {
    if (!e.currentTarget.dataset.item.IsRead) {
      let list = this.data.list
      list[e.currentTarget.dataset.index].velue = true
    let isall =    list.every(velue =>{
         return velue.IsRead
      })
      if(isall){
        this.getTabBar().readMseege();
      }
      this.setData({
        list: list
      })
    }
    wx.navigateTo({
      url: `./particularsItem/particular?id=${e.currentTarget.dataset.item.ID}&content=${e.currentTarget.dataset.item.Content}&IsRead=${e.currentTarget.dataset.item.IsRead}&time=${e.currentTarget.dataset.item.CreateTime}`
    })
  },

  signRead() {
    if (!this.data.list.length) {
      return
    }
    information.bulkreadmessage({}).then(() => {
      let list = this.data.list
      list.forEach(velue => {
        velue.IsRead = true
      })
      this.setData({
        list: list
      })
      this.getTabBar().readMseege();
      wx.showToast({
        title: '全部已读成功',
        duration: 1000,
        icon: "none",
      })
      // this.data.PageIndex = 1

      // this.init()
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  formatTime(date) {
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const hour = date.getHours();
    const minute = date.getMinutes();
    const second = date.getSeconds();

    return `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day} ${hour < 10 ? '0' + hour : hour}:${minute < 10 ? '0' + minute : minute}`;
  },

  init() {
    information.gettosystemnoticemessagebypage({
      PageIndex: this.data.PageIndex,
      PageSize: this.data.PageSize,
    }).then((res) => {
      this.setData({
        loading: true
      })
      if (res.Result.Datas.length) {
        res.Result.Datas.forEach((item) => {
          item.CreateTime = this.formatTime(new Date(item.CreateTime))
        })
        this.setData({
          list: [...this.data.list, 
            ...res.Result.Datas,
          ],
        })
        this.data.PageIndex = this.data.PageIndex + 1
        if (this.data.list.length >= res.Result.TotalCount) {
          this.setData({
            loading: true
          })
        } else {
          this.setData({
            loading: false
          })
        }

      }

    })
  },

  onLoad(options) {
  
    if(!wx.getStorageSync('isRead')){
      this.init()
    }
   
    const app = getApp()
    wx.getSystemInfo({
      success: res => {
        let systemInfo = wx.getSystemInfoSync()
        let info = wx.getMenuButtonBoundingClientRect() 
        this.setData({
          height:(info.top-systemInfo.statusBarHeight)*2+ info.height,
          dynamicStyle: (res.statusBarHeight) + "px",
          dayStyle: (info.top-systemInfo.statusBarHeight)*2+ info.height + res.statusBarHeight + "px",
        })
      },
      fail(err) {
        console.log(err);
      }
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
   this.getTabBar().init();

   if(wx.getStorageSync('isRead')){
    this.setData({
      PageIndex:1,
      list:[],
      loading:false,
    })
    this.init()
   }
 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.loading == true) {
      return
    }
    this.init()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})